/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.chlitina.store.modules.expert.utils;

import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import net.sf.json.JSONArray;

import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.modules.base.entity.CustomerEmployeeRelationship;
import com.chlitina.store.modules.base.service.CustomerEmployeeRelationshipService;
import com.chlitina.store.modules.employee.entity.Employee;
import com.chlitina.store.modules.employee.service.EmployeeService;
import com.chlitina.store.modules.expert.entity.Care;
import com.chlitina.store.modules.expert.entity.CustomerExam;
import com.chlitina.store.modules.expert.entity.CustomerSolution;
import com.chlitina.store.modules.expert.entity.CustomerSolutionAndCare;
import com.chlitina.store.modules.expert.entity.CustomerSolutionAndProduct;
import com.chlitina.store.modules.expert.entity.CustomerSolutionMix;
import com.chlitina.store.modules.expert.entity.Product;
import com.chlitina.store.modules.expert.entity.SolutionFace;
import com.chlitina.store.modules.expert.entity.SolutionFaceAndProduct;
import com.chlitina.store.modules.expert.entity.SolutionFaceCare;
import com.chlitina.store.modules.expert.entity.SolutionFaceFood;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductMix;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductMixDetail;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductStep;
import com.chlitina.store.modules.expert.entity.SolutionFaceStoreType;
import com.chlitina.store.modules.expert.entity.SystemDefinitionAreaSeason;
import com.chlitina.store.modules.expert.service.SolutionFaceAndProductService;
import com.chlitina.store.modules.expert.service.SolutionFaceCareService;
import com.chlitina.store.modules.expert.service.SolutionFaceFoodService;
import com.chlitina.store.modules.expert.service.SolutionFaceProductMixDetailService;
import com.chlitina.store.modules.expert.service.SolutionFaceProductMixService;
import com.chlitina.store.modules.expert.service.SolutionFaceService;
import com.chlitina.store.modules.sys.dao.AreaDao;
import com.chlitina.store.modules.sys.entity.Area;
import com.chlitina.store.modules.sys.entity.Dict;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.service.SettingsService;
import com.chlitina.store.modules.sys.utils.DictUtils;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 用户工具类
 * @author ThinkGem
 * @version 2013-4-21
 */
@Service
public class ExpertUtils implements ApplicationContextAware {
	private static org.slf4j.Logger logger = LoggerFactory.getLogger(ExpertUtils.class);
	private static AreaDao areaDao;
	private static EmployeeService employeeService;
	private static SolutionFaceService solutionFaceService;
	private static SettingsService settingsService;
	private static SolutionFaceCareService solutionFaceCareService;
	private static SolutionFaceFoodService solutionFaceFoodService;
	private static SolutionFaceAndProductService solutionFaceAndProductService;
	private static SolutionFaceProductMixService solutionFaceProductMixService;
	private static SolutionFaceProductMixDetailService solutionFaceProductMixDetailService;
	private static CustomerEmployeeRelationshipService customerEmployeeRelationshipService;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext){
		areaDao = (AreaDao)applicationContext.getBean("areaDao");
		employeeService = (EmployeeService)applicationContext.getBean("employeeService");
		solutionFaceService = (SolutionFaceService)applicationContext.getBean("solutionFaceService");
		solutionFaceCareService = (SolutionFaceCareService)applicationContext.getBean("solutionFaceCareService");
		solutionFaceFoodService = (SolutionFaceFoodService)applicationContext.getBean("solutionFaceFoodService");
		settingsService = (SettingsService)applicationContext.getBean("settingsService");
		solutionFaceAndProductService = (SolutionFaceAndProductService)applicationContext.getBean("solutionFaceAndProductService");
		solutionFaceProductMixService = (SolutionFaceProductMixService)applicationContext.getBean("solutionFaceProductMixService");
		solutionFaceProductMixDetailService = (SolutionFaceProductMixDetailService)applicationContext.getBean("solutionFaceProductMixDetailService");
		customerEmployeeRelationshipService = (CustomerEmployeeRelationshipService)applicationContext.getBean("customerEmployeeRelationshipService");
	}
	
	/**
	 * 去掉重复的方案
	 */
	public static List<Map<String, Object>> removeSameSolution(List<Map<String, Object>> list){
		List<Long> exist = Lists.newArrayList();
		List<Map<String, Object>> existList = Lists.newArrayList();
		for (Map<String, Object> item : list) {
			SolutionFace s = (SolutionFace)item.get("SolutionFace");
			long id = s.getId();
			if(exist.contains(id)) {
				existList.add(item);
			} else {
				exist.add(id);
			}
		}
		if (existList != null && existList.size() > 0) {
			list.removeAll(existList);
		}
		return list;
	}
	/**
	 * 获取区域
	 */
	public static Map<Long, String> getLocation() {
		Map<Long, String> areaMap = Maps.newLinkedHashMap();
		long id = 1;
		List<Area> areaList = areaDao.findByParent(id);
		for (Area area : areaList) {
			areaMap.put(area.getId(), area.getName());
		}
		return areaMap;
	}

	/**
	 * 去掉不符合条件的方案
	 */
	public static boolean checkLocationAndSeason(SolutionFace solutionFace, Long storeTypeId){
		if(solutionFace.getStatus() == SolutionFace.FLAG_DISABLE){
			return false;
		}
		String storeTypeIds = "";
		for(SolutionFaceStoreType cst : solutionFace.getStoreTypeList()){
			storeTypeIds += cst.getStoreType().getId()+ ",";
		}
		if(!storeTypeIds.contains(storeTypeId.toString())){
			return false;
		}
		return true;
	}
	
	/**
	 * 按从小到大排序
	 */
	public static class ComparatorAsc implements Comparator {
		public int compare(Object arg0, Object arg1) {
			Map<String, Object> user0 = (Map<String, Object>) arg0;
			Map<String, Object> user1 = (Map<String, Object>) arg1;

			return ((Integer)user0.get("priority")).compareTo((Integer)user1.get("priority"));
		}
	}
	/**
	 * 按从大到小排序
	 */
	public static class ComparatorDesc implements Comparator {
		public int compare(Object arg0, Object arg1) {
			Map<String, Object> user0 = (Map<String, Object>) arg0;
			Map<String, Object> user1 = (Map<String, Object>) arg1;

			return ((Integer)user1.get("priority")).compareTo((Integer)user0.get("priority"));
		}
	}
	/**
	 * 0排第一位， 其它按从大到小排序
	 */
	public static class ComparatorDescZero implements Comparator {
		public int compare(Object arg0, Object arg1) {
			Map<String, Object> user0 = (Map<String, Object>) arg0;
			Map<String, Object> user1 = (Map<String, Object>) arg1;
			if((Integer)user0.get("priority") == 0) {
				return -1;
			}
			if((Integer)user1.get("priority") == 0) {
				return 1;
			}
			return ((Integer)user1.get("priority")).compareTo((Integer)user0.get("priority"));
		}
	}
	
	public static boolean ifShow(int index, boolean ifHaveBasic, int length){
		int showTabs = 3;//最多显示的方案个数
		if(ifHaveBasic) {
			if(index < (showTabs-1)) {
				return true;
			}
			if(index == (length-1)) {
				return true;
			}
		} else {
			if(index < showTabs) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 获取数据
	 */
	public static List getSearchJsonData(boolean ifHaveBasic, List<Map<String, Object>> list,
			Long locationSeason){
		List<Map<String, Object>> dataList = Lists.newArrayList();
		List<Product> listProduct = Lists.newArrayList();
		
		//去重複，按优先级排序，基本方案最后
		list = removeSameSolution(list);
		ComparatorDesc comparator= new ComparatorDesc();
		Collections.sort(list, comparator);
		
		int index = 0;
		int length = list.size();
		for (Map<String, Object> item : list) {
			int num = index;
			if(ifHaveBasic) {
				if(index == (length-1)) {
					num = 0;
				} else {
					num = index + 1;
				}
			}
			Map<String, Object> data = getMapData(item, listProduct, num, locationSeason);
			if(data != null) {
				dataList.add(data);
			}
			index++;
		}
		//把基本方案移到第一位
		ComparatorDescZero comparatorZero= new ComparatorDescZero();
		Collections.sort(dataList, comparatorZero);
		return dataList;
	}
	
	public static Map<String, Object> getMapData(Map<String, Object> item, 
			List<Product> listProduct, int index, Long locationSeason){
		long storeTypeId = StoreUtils.getStore().getStoreType().getId();
		SolutionFace s = (SolutionFace)item.get("SolutionFace");
		if(!checkLocationAndSeason(s, storeTypeId)){
			return null;
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("index", index);
		map.put("priority", item.get("priority"));
		map.put("skinType", item.get("skinType"));
		map.put("solutionId", s.getId());
		map.put("solutionName", s.getName());
		map.put("solutionUsage", s.getUsage());
		map.put("solutionMutex", s.getMutexSolutionIds());
		List<SolutionFaceProductStep> stepList = s.getStepList();
		map.put("stepLength", stepList.size());
		List<Map<String, Object>> stepMapList = Lists.newArrayList();
		for (SolutionFaceProductStep step : stepList) {
			Map<String, Object> setpMap = Maps.newHashMap();
			setpMap.put("step", step.getStep());
			setpMap.put("stepText", "步骤" + step.getStep());
			List<Map<String, Object>> mixMapList = Lists.newArrayList();
			Map<String, Object> dayMixMap = Maps.newHashMap();
			Map<String, Object> nightMixMap = Maps.newHashMap();
			List<Map<String, Object>> dayMixMapList = Lists.newArrayList();
			List<Map<String, Object>> nightMixMapList = Lists.newArrayList();
			List<SolutionFaceProductMix>  mixList = solutionFaceProductMixService.getByStep(step.getId(), locationSeason, storeTypeId);
			for (SolutionFaceProductMix mix : mixList) {
				Map<String, Object> mixMap = Maps.newHashMap();
				mixMap.put("mixId", mix.getId());
				mixMap.put("mixNeeded", mix.getNeeded());
				int dayOrNight = mix.getDay_or_night();
				mixMap.put("mixDayOrNight", dayOrNight);
				List<Map<String, Object>> detailMapList = Lists.newArrayList();
				List<SolutionFaceProductMixDetail>  detailList = solutionFaceProductMixDetailService.getByMix(mix.getId(), storeTypeId);
				for (SolutionFaceProductMixDetail detail : detailList) {
					Product product = detail.getProduct();
					Map<String, Object> detailMap = Maps.newHashMap();
					detailMap.put("productMixRatio", detail.getMix_ratio());
					detailMap.put("productId", product.getId());
					detailMap.put("productName", product.getName());
					String imageUrl = "#";
					if(!product.getImage().equals("")) {
						imageUrl = Global.getConfig("ossPath") + product.getImage();
					}
					detailMap.put("productImage", imageUrl);
					detailMap.put("productPrice", product.getRetail_price());
					SolutionFaceAndProduct sp = solutionFaceAndProductService.getOneByPK(s.getId(), product.getId());
					detailMap.put("productVolume", sp.getVolume());
					detailMap.put("productAmount", sp.getAmount());
					detailMap.put("productDuration", sp.getDuration());
					detailMapList.add(detailMap);
				}
				mixMap.put("detailList", detailMapList);
				if(dayOrNight == 0) {
					int size = dayMixMapList.size();
					size++;
					mixMap.put("mixName", "日间搭配组合" + size);
 					dayMixMapList.add(mixMap);	
				} else {
					int size = nightMixMapList.size();
					size++;
					mixMap.put("mixName", "夜间搭配组合" + size);
					nightMixMapList.add(mixMap);
				}
			}
			dayMixMap.put("dayText", "日间");
			dayMixMap.put("mixList", dayMixMapList);
			nightMixMap.put("dayText", "夜间");
			nightMixMap.put("mixList", nightMixMapList);
			mixMapList.add(dayMixMap);
			mixMapList.add(nightMixMap);
			setpMap.put("mixList", mixMapList);
			stepMapList.add(setpMap);
		}
		map.put("stepList", stepMapList);
		List<SolutionFaceFood> foodList =  solutionFaceFoodService.getBySolutionFace(s.getId(), locationSeason, storeTypeId);
		List<Map<String, Object>> foodMapList = Lists.newArrayList();
		for (SolutionFaceFood solutionFaceFood : foodList) {
			Product product = solutionFaceFood.getProduct();
			Map<String, Object> detailMap = Maps.newHashMap();
			detailMap.put("productId", product.getId());
			detailMap.put("solutionId", solutionFaceFood.getSolutionFace().getId());
			detailMap.put("productName", product.getName());
			String imageUrl = "#";
			if(!product.getImage().equals("")) {
				imageUrl = Global.getConfig("ossPath") + product.getImage();
			}
			detailMap.put("productImage", imageUrl);
			detailMap.put("productPrice", product.getRetail_price());
			detailMap.put("productAmount", solutionFaceFood.getAmount());
			detailMap.put("productDuration", solutionFaceFood.getDuration());
			foodMapList.add(detailMap);
		}
		map.put("foodList", foodMapList);
		List<SolutionFaceCare> careList = solutionFaceCareService.getBySolutionFace(s.getId(), locationSeason, storeTypeId);
		List<Map<String, Object>> careMapList = Lists.newArrayList();
		for (SolutionFaceCare solutionFaceCare : careList) {
			Care care = solutionFaceCare.getCare();
			Map<String, Object> careMap = Maps.newHashMap();
			careMap.put("careId", care.getId());
			careMap.put("careName", care.getName());
			careMap.put("careNeeded",solutionFaceCare.getNeeded());
			careMap.put("careDuration",care.getDuration());
			careMap.put("carePrice",care.getPrice());
			careMap.put("careTimes",care.getTimes());
			careMapList.add(careMap);
		}
		map.put("careList", careMapList);
		return map;
	}
	
	public static Map<Long, String> getSeason(Long areaId) {
		Map<Long, String> seasonMap = Maps.newLinkedHashMap();
		Area area = areaDao.findOne(areaId);
		if(area != null){
			List<SystemDefinitionAreaSeason> seasonList = area.getSeasonList();
			for (SystemDefinitionAreaSeason season : seasonList) {
				seasonMap.put(season.getId(), season.getName());
			}
		}
		return seasonMap;
	}
	
	public static Map<Long, String> getCustomerEmployee(Long customerId) {
		Map<Long, String> employeeMap = Maps.newLinkedHashMap();
		Store store = StoreUtils.getStore();
		List<Employee> employeeList = employeeService.findByStore(store, Employee.STATUS_WORKING);
		if (customerId != null) {
			List<CustomerEmployeeRelationship> cerList = customerEmployeeRelationshipService.findByCustomer(customerId);
			for (CustomerEmployeeRelationship cer : cerList) {
				employeeMap.put(cer.getEmployee().getId(), cer.getEmployee().getName());
			}
		}
		for (Employee employee : employeeList) {
			if (employeeMap.containsKey(employee.getId())) {
				continue;
			}
			employeeMap.put(employee.getId(), employee.getName());
		}
		return employeeMap;
	}
	public static Map<Long, String> getEmployee() {
		Map<Long, String> employeeMap = Maps.newLinkedHashMap();
		Store store = StoreUtils.getStore();
		List<Employee> employeeList = employeeService.findByStore(store, Employee.STATUS_WORKING);
		for (Employee employee : employeeList) {
			employeeMap.put(employee.getId(), employee.getName());
		}
		return employeeMap;
	}
	
	public static Map<Long, String> getLeaveEmployee() {
		Map<Long, String> employeeMap = Maps.newLinkedHashMap();
		Store store = StoreUtils.getStore();
		List<Employee> employeeList = employeeService.findByStore(store, Employee.STATUS_NONWORKING);
		for (Employee employee : employeeList) {
			employeeMap.put(employee.getId(), employee.getName());
		}
		return employeeMap;
	}
	
	public static void setPageNoByCurrentId(Page page, int index){
		int pageSize = page.getPageSize();
        int pageNo = (index-1)/pageSize +1;
        page.setPageNo(pageNo);
	}
	
	/**
	 * 获取当前店面和季节的方案
	 */
	public static Map<Long, String> getSolutionByStore() {
		Map<Long, String> solutionFaceMap = Maps.newLinkedHashMap();
		List<SolutionFace> list = solutionFaceService.findAll();
		for (SolutionFace solutionFace : list) {
			solutionFaceMap.put(solutionFace.getId(), solutionFace.getName());
		}
		return solutionFaceMap;
	}
	
	/**
	 * 获取一级区域
	 */
	public static Area getFirstArea(Area area) {
		if(area.getId() == 1) {
			return area;
		}
		while(area.getParent().getId() != 1) {
			area = area.getParent();
		}
		return area;
	}
	
	public static Map<String, String> getSolutionPriority() {
		Map<String,String> priorityMap = new TreeMap<String,String>(); 
		List<Dict> dictList = DictUtils.getDictList("parameter_category_priority");
		for (Dict dict : dictList) {
			priorityMap.put(dict.getDesciption(), dict.getLabel());
		}
		return priorityMap;
	}
	
//	1.在所有包含有该产品的步骤的产品组合里，均参与了混合配比
//	 - 程序自动计算使用日期（容量参与计算）
//
//	2.在所有包含有该产品的步骤的产品组合里，均不参与混合配比
//	 - 使用固定的产品日期（solution_face_product.duration）
//
//	3.在所有包含有该产品的步骤的产品组合里，即参与了混合配比，又有不参与混合配比的用法
//	 - 使用固定的产品日期（solution_face_product.duration）
	public static int getMixProductDuration(SolutionFace solutionFace, long productId, int productAmount, 
			int productVolume, List<Long> mixListArray) {
		if(productVolume == 0) {
			return -1;
		}
		int times = solutionFace.getCare_times_per_day();
		int mix = settingsService.getIntValue("SET_MIX_PRODUCT_DOSAGE_PER_TIME");
		double oneDayUsed = 0;
		
		for (int i = 0; i < mixListArray.size(); i++) {
			float per = 0;
			float allPer = 0;
			long mixId = (Long) mixListArray.get(i);
			SolutionFaceProductMix sm = solutionFaceProductMixService.get(mixId);
			List<SolutionFaceProductMixDetail> slist = sm.getDetailList();
			for (SolutionFaceProductMixDetail smd : slist) {
				if(smd.getProduct().getStatus() == Product.FLAG_DISABLE) {
					continue;
				}
				float mixRatio = smd.getMix_ratio();
				if(smd.getProduct().getId() == productId) {
					if(mixRatio == -1) {
						return -1;
					}
					per = mixRatio;
				}
				if(mixRatio != -1) {
					allPer += mixRatio;
				}
			}
			if(per != 0 && per != -1) {
				if(sm.getDay_or_night() == SolutionFaceProductMix.TYPE_DAY) {
					oneDayUsed += (times-1)*mix*per/allPer;
				} else {
					oneDayUsed += mix*per/allPer;
				}
			}
		}
		if(oneDayUsed == 0) {
			return -1;
		}
		int days = (int) Math.floor(productVolume*productAmount/oneDayUsed);
		return days;
	}
	
	public static int getMixProductDuration(CustomerSolutionAndProduct c, int amount) {
		long productId = c.getStoreProduct().getProduct().getId();
		SolutionFace solutionFace = c.getCustomerSolution().getSolutionFace();
		SolutionFaceAndProduct sp = solutionFaceAndProductService.getOneByPK(solutionFace.getId(), productId);
		List<Long> mixListArray = Lists.newArrayList();
		List<CustomerSolutionMix>  list = c.getCustomerSolution().getMixList();
		for (CustomerSolutionMix customerSolutionMix : list) {
			mixListArray.add(customerSolutionMix.getSolutionFaceProductMix().getId());
		}
		int productVolume = sp.getVolume();
		
		int days = getMixProductDuration(solutionFace, productId, amount, productVolume, mixListArray);
		if(days == -1) {
			days = sp.getDuration()*amount;
		}
		return days;
	}
	
	public static Date getNotifyDate(int duration, int tipDays) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, duration-tipDays);
		return cal.getTime();
	}
	
	public static boolean ifMixExist(List<CustomerSolutionMix> existMixList, long id) {
		for (CustomerSolutionMix c : existMixList) {
			if(c.getSolutionFaceProductMix().getId() == id) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean ifProductExist(List<CustomerSolutionAndProduct> existProductList, long id) {
		for (CustomerSolutionAndProduct p : existProductList) {
			if(p.getStoreProduct().getProduct().getId() == id) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean ifCareExist(List<CustomerSolutionAndCare> existCareList, long id) {
		for (CustomerSolutionAndCare c : existCareList) {
			if(c.getStoreCare().getCare().getId() == id) {
				return true;
			}
		}
		return false;
	}
	
	public static List<Map<String, Object>> getCustomerSolutionMapData(CustomerExam customerExam){
		List<Map<String, Object>> dataList = Lists.newArrayList();
		List<CustomerSolution> list = customerExam.getCustomerSolutionList();
		int index = 0;
		for (CustomerSolution customerSolution : list) {
			if(customerSolution.getStatus() == CustomerSolution.UNADOPTED_STATUS) {
				continue;
			}
			List<CustomerSolutionMix> existMixList = customerSolution.getMixList();
			List<CustomerSolutionAndCare> existCareList = customerSolution.getCareList();
			List<CustomerSolutionAndProduct> existProductList = customerSolution.getProductList();
			SolutionFace s = customerSolution.getSolutionFace();
			Map<String, Object> map = Maps.newHashMap();
			map.put("index", index);
			map.put("solutionId", s.getId());
			map.put("solutionName", s.getName());
			map.put("solutionUsage", s.getUsage());
			map.put("solutionMutex", s.getMutexSolutionIds());
			List<SolutionFaceProductStep> stepList = s.getStepList();
			map.put("stepLength", stepList.size());
			List<Map<String, Object>> stepMapList = Lists.newArrayList();
			for (SolutionFaceProductStep step : stepList) {
				Map<String, Object> setpMap = Maps.newHashMap();
				setpMap.put("step", step.getStep());
				setpMap.put("stepText", "步骤" + step.getStep());
				List<Map<String, Object>> mixMapList = Lists.newArrayList();
				Map<String, Object> dayMixMap = Maps.newHashMap();
				Map<String, Object> nightMixMap = Maps.newHashMap();
				List<Map<String, Object>> dayMixMapList = Lists.newArrayList();
				List<Map<String, Object>> nightMixMapList = Lists.newArrayList();
				List<SolutionFaceProductMix>  mixList = step.getMixList();
				for (SolutionFaceProductMix mix : mixList) {
					if(!ifMixExist(existMixList, mix.getId())) {
						continue;
					}
					Map<String, Object> mixMap = Maps.newHashMap();
					mixMap.put("mixId", mix.getId());
					mixMap.put("mixNeeded", mix.getNeeded());
					int dayOrNight = mix.getDay_or_night();
					mixMap.put("mixDayOrNight", dayOrNight);
					List<Map<String, Object>> detailMapList = Lists.newArrayList();
					List<SolutionFaceProductMixDetail>  detailList = mix.getDetailList();
					for (SolutionFaceProductMixDetail detail : detailList) {
						Product product = detail.getProduct();
						if(!ifProductExist(existProductList, product.getId())) {
							continue;
						}
						Map<String, Object> detailMap = Maps.newHashMap();
						detailMap.put("productMixRatio", detail.getMix_ratio());
						detailMap.put("productId", product.getId());
						detailMap.put("productName", product.getName());
						String imageUrl = "#";
						if(!product.getImage().equals("")) {
							imageUrl = Global.getConfig("ossPath") + product.getImage();
						}
						detailMap.put("productImage", imageUrl);
						detailMap.put("productPrice", product.getRetail_price());
						SolutionFaceAndProduct sp = solutionFaceAndProductService.getOneByPK(s.getId(), product.getId());
						detailMap.put("productVolume", sp.getVolume());
						detailMap.put("productAmount", sp.getAmount());
						detailMap.put("productDuration", sp.getDuration());
						detailMapList.add(detailMap);
					}
					mixMap.put("detailList", detailMapList);
					if(dayOrNight == 0) {
						int size = dayMixMapList.size();
						size++;
						mixMap.put("mixName", "日间搭配组合" + size);
	 					dayMixMapList.add(mixMap);	
					} else {
						int size = nightMixMapList.size();
						size++;
						mixMap.put("mixName", "夜间搭配组合" + size);
						nightMixMapList.add(mixMap);
					}
				}
				dayMixMap.put("dayText", "日间");
				dayMixMap.put("mixList", dayMixMapList);
				nightMixMap.put("dayText", "夜间");
				nightMixMap.put("mixList", nightMixMapList);
				mixMapList.add(dayMixMap);
				mixMapList.add(nightMixMap);
				setpMap.put("mixList", mixMapList);
				stepMapList.add(setpMap);
			}
			map.put("stepList", stepMapList);
			List<SolutionFaceFood> foodList =  s.getFoodList();
			List<Map<String, Object>> foodMapList = Lists.newArrayList();
			for (SolutionFaceFood solutionFaceFood : foodList) {
				Product product = solutionFaceFood.getProduct();
				if(!ifProductExist(existProductList, product.getId())) {
					continue;
				}
				Map<String, Object> detailMap = Maps.newHashMap();
				detailMap.put("productId", product.getId());
				detailMap.put("productName", product.getName());
				String imageUrl = "#";
				if(!product.getImage().equals("")) {
					imageUrl = Global.getConfig("ossPath") + product.getImage();
				}
				detailMap.put("productImage", imageUrl);
				detailMap.put("productPrice", product.getRetail_price());
				detailMap.put("productAmount", solutionFaceFood.getAmount());
				detailMap.put("productDuration", solutionFaceFood.getDuration());
				foodMapList.add(detailMap);
			}
			map.put("foodList", foodMapList);
			List<SolutionFaceCare> careList = s.getCareList();
			List<Map<String, Object>> careMapList = Lists.newArrayList();
			for (SolutionFaceCare solutionFaceCare1 : careList) {
				if(!ifCareExist(existCareList, solutionFaceCare1.getCare().getId())) {
					continue;
				}
				Care care = solutionFaceCare1.getCare();
				Map<String, Object> careMap = Maps.newHashMap();
				careMap.put("careId", care.getId());
				careMap.put("careName", care.getName());
				careMap.put("careNeeded",solutionFaceCare1.getNeeded());
				careMap.put("careDuration",care.getDuration());
				careMap.put("carePrice",care.getPrice());
				careMap.put("careTimes",care.getTimes());
				careMapList.add(careMap);
			}
			map.put("careList", careMapList);
			dataList.add(map);
			index++;
		}
		return dataList;
	}
	
	public static List<Long> toLongList(JSONArray mixList) {
		String [] mixListString = (String[]) JSONArray.toArray(mixList, new String(), null);
		List<Long> list = Lists.newArrayList();
		for (int i = 0; i < mixListString.length; i++) {
			list.add(Long.parseLong(mixListString[i]));
		}
		return list;
	}
}
